package com.ly.im.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ly.im.pojo.Message;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * @Author LY
 * @create 2024/2/2 16:10
 * @Description 消息
 */
public interface MessageRepository extends BaseMapper<Message> {

    @Select("SELECT a.* FROM ( " +
            "SELECT a.*,b.image FROM message AS `a` " +
            "LEFT JOIN `user` AS `b` ON a.`from` = b.id " +
            "WHERE `from` = #{from} and `to` = #{to}\n" +
            "UNION\n" +
            "SELECT a.*,b.image FROM message AS `a` " +
            "LEFT JOIN `user` AS `b` ON a.`from` = b.id " +
            "WHERE `from` = #{to} and `to` = #{from} ) as `a` " +
            "ORDER BY a.time ASC")
    List<Map> list(@Param("from")Long id, @Param("to")Long to);

    @Insert("insert into message(`from`,`to`,`type`,content,`time`) " +
            "values(#{from},#{to},#{type},#{content},#{time})")
    int saveMessage(Message message);
}
